import React, { useState } from 'react';
import { Form, Input, message, Divider, DatePicker, Select, Button } from 'antd';
import { } from '@/utils/utils';
import { validPassWord } from '@/services/user';
import { useRequest, useModel } from 'umi';
import { updatePassword } from '@/services/user';
import styles from '@/global.less';


const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};



const UpdateForm: React.FC = (props) => {


  const [form] = Form.useForm();
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};


  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
    fieldsValue.userId = currentUser?.userId;
    console.log(fieldsValue)
    const hide = message.loading('正在修改密码');
    try {
      hide();
      await updatePassword(fieldsValue);
      message.success('密码修改成功！');
    } catch (error) {
      hide();
      message.error('重置密码失败请重试！');
    }
    form.resetFields();
  };

  const validatorOldPassWord = async (_: any, value: string) => {
    const json = await validPassWord({ userId: currentUser?.userId, password: value });
    const promise = Promise;
    if (json && json == "密码错误") {
      return promise.reject(json);
    }
    return promise.resolve();
  };
  return (
    <div style={{ border: '1px solid rgb(53 95 184)', margin: '16px', padding: '24px', }}>
      <Form
        {...formLayout}
        form={form}
        onFinish={okHandle}
        initialValues={{

        }}
      >

        <Form.Item
          name="oldpassword"
          label="旧密码"
          rules={[
            {
              required: true,
              message: '请输入旧密码!',
            }, { validator: validatorOldPassWord }
          ]}
          hasFeedback
        >
          <Input.Password />
        </Form.Item>
        <Form.Item
          name="password"
          label="新密码"
          rules={[
            {
              required: true,
              message: '请输入新密码!',
            },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('oldpassword') != value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('新密码和旧密码不能相同!'));
              },
            }),
          ]}
          hasFeedback
        >
          <Input.Password />
        </Form.Item>

        <Form.Item
          name="newPassword"
          label="再次输入密码"
          dependencies={['password']}
          hasFeedback
          rules={[
            {
              required: true,
              message: '请再次输入密码!',
            },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('password') === value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('两次密码输入不一致!'));
              },
            }),
          ]}
        >
          <Input.Password />
        </Form.Item>
        <Divider />
        <Form.Item style={{ textAlign: 'center' }}>
          <Button type="primary" htmlType="submit">
            提交
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};

export default UpdateForm;
